package BlockQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_DaChiZi
 * Date: 2025/6/23
 * Time: 13:34
 */

class MyBlockingQueue {
    private String[] data = null;
    public volatile int head = 0;
    public volatile int tail = 0;
    public volatile int size = 0;

    public MyBlockingQueue (int capacity) {
        data = new String[capacity];
    }

    public void put(String s) throws InterruptedException {
        synchronized (this) {
            while (size == data.length) {
                this.wait();
            }
            data[tail] = s;
            tail++;
            if (tail >= data.length) {
                tail = 0;
            }
            size++;
            this.notify();
        }
    }

    public String take() throws InterruptedException {
        String ret = "";
        synchronized (this) {
            while (size == 0) {
                this.wait();
            }
            ret = data[head];
            head++;
            if (head >= data.length) {
                head = 0;
            }
            size--;
            this.notify();

        }
        return ret;
    }
}

public class Demo3 {
    public static void main(String[] args) {

    }
}
